﻿using System;
using System.Collections.ObjectModel;
using System.Web;
using System.Web.UI;
using Common;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxTabControl;
using FlexySys.Web.Core;


namespace Web.Document
{
    public partial class Default : DocumentPage
    {

        string _tabsInSession;
       
        ObservableCollection<TabDocument> _tabCollection;
        protected void Page_Load(object sender, EventArgs e)
        {
           tabs.TabPages.Clear();
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                _tabsInSession = HttpContext.Current.User.Identity.Name + "_Tabs";
            }
            if (Session[_tabsInSession] == null)
            {
                _tabCollection = new ObservableCollection<TabDocument>();
                Session[_tabsInSession] = _tabCollection;
                _tabCollection.Add(new TabDocument
                {
                    Text = "Document 1",
                    TabContent = LoadControl(@"\FlexyUserControls\DocumenContentControl.ascx")
                });
                _tabCollection.Add(new TabDocument
                                       {
                                           Text = "Document 2",
                                           TabContent =
                                               LoadControl(@"\FlexyUserControls\DocumenContentControl.ascx")
                                       });
                _tabCollection.Add(new TabDocument { Text = "Document 3" });
                _tabCollection.Add(new TabDocument { Text = "Document 4" });
            }
            else
            {
                _tabCollection = Session[_tabsInSession] as ObservableCollection<TabDocument>;
            }

           
                CreateTabPages();

        }

        private void CreateTabPages()
        {
            tabs.TabPages.Clear();
            foreach (var o in _tabCollection)
            {
                var page = new TabPage
                               {
                                      Text = o.Text

                                  };
                if (o.TabContent != null)
                    page.Controls.Add(o.TabContent);

                tabs.TabPages.Add(page);
            }
            //this.tabs.DataSource = _tabCollection;
            //this.tabs.DataBind();
        }

     



        protected void OnClick(object sender, ImageClickEventArgs e)
        {
           
        }

        protected void CallbackPanelCallback(object sender, CallbackEventArgsBase e)
        {
            
            tabs.TabPages.RemoveAt(int.Parse(e.Parameter) );
            _tabCollection.RemoveAt(int.Parse(e.Parameter));
           
        }
    }
}